﻿using Lamex.mSA.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Lamex.mSA.Calculate
{
   public class Fuc_CBM
    {
        /// <summary>
        /// [产品尺寸法CBM解析计算]，返回[长、宽、高、CBM]，即根据产品尺寸解析并计算CBM
        /// </summary>
        /// <param name="strProductSize"></param>
        /// <returns></returns>
        public static Tuple<decimal, decimal, decimal, decimal> Product_Size_CBM(string strProductSize,decimal Length_Allowance,decimal Width_Allowance,decimal Thick_Allowance)
        {
            //转为大写字符串
            strProductSize = strProductSize.ToUpper();
            //采用正则表达式把非数字转换成特殊字符"_"
            string[] list = Regex.Replace(strProductSize, @"[^\d.]", "_").Split('_');
            //取前面三个数字值作为【长】【宽】【厚】,少于三个则不取，多于三个只取前三个
            List<decimal> sizeList = new List<decimal>();
            decimal Length = 0, Width = 0, Thick = 0, VOLUME = 0;
           // int Length_Allowance = 20, Width_Allowance = 20, Thick_Allowance = 5;
            for (int i = 0; i < list.Length; i++)
            {
                if (list[i].Length > 0)
                    sizeList.Add(Convert.ToDecimal(list[i]));
            }
            if (sizeList.Count >= 3)
            {
                if (strProductSize.Contains("CM"))
                {
                    Length = sizeList[0] * 10 + Length_Allowance;
                    Width = sizeList[1] * 10 + Width_Allowance;
                    Thick = sizeList[2] * 10 + Thick_Allowance;
                    VOLUME = (Length * Width * Thick) / 1000000000;
                }
                else
                {
                    Length = sizeList[0] + Length_Allowance;
                    Width = sizeList[1] + Width_Allowance;
                    Thick = sizeList[2] + Thick_Allowance;
                    VOLUME = (Length * Width * Thick) / 1000000000;
                }
            }
            Tuple<decimal, decimal, decimal, decimal> tuple = new Tuple<decimal, decimal, decimal, decimal>(Length, Width, Thick, VOLUME);
            return tuple;
        }

        /// <summary>
        /// 纸箱CBM解析
        /// </summary>
        /// <param name="dt_MO">MO_BOM表</param>
        /// <param name="dt_Mo_Main">生产订单表</param>
        /// <param name="dt_CartonBom">返回符合条件的纸箱清单表</param>
        /// <returns></returns>
        public static List<CartonMaterial> CARTON_PACKING_CBM(DataTable dt_MO,DataTable dt_Mo_Main,out DataTable dt_CartonBom)
        {
            List<CartonMaterial> cartons = new List<CartonMaterial>();
            DataTable dt_Carton_Mo = new DataTable();//返回符合的纸箱BOM号码
            //纸箱描述表
            string sql_Carton = @"SELECT [Carton_Name] FROM [LamexMESManager].[dbo].[SA_CBM_Carton]";
            DataTable dt_CartonDesc = Lamex.Base.BaseBuildSql.GetDataTableBySql(sql_Carton);

            List<string> list = dt_CartonDesc.AsEnumerable().Select(d => d.Field<string>("Carton_Name")).ToList();
         

            var query = (from d in dt_MO.AsEnumerable()
                         from l in list
                         where d.Field<string>("Material_Desc").Contains(l) 
                         select d).ToList();

            
            //if (dt_MO.Select("Material_Desc like '%纸箱%' or Material_Desc like '%折盒%' " +
            //    "or Material_Desc like '%天地盒%'  or Material_Desc like '%天地盖%'  or Material_Desc like '%侧封箱%' " +
            //    "or Material_Desc like '%中封箱%' or Material_Desc like '%普通箱%' ").Length > 0)
            if(query.Count>0)
            {
                //DataTable dt_MO_Temp = dt_MO.Select("Material_Desc like '%纸箱%' or Material_Desc like '%折盒%' or Material_Desc like '%天地盒%'  or Material_Desc like '%天地盖%'  or Material_Desc like '%侧封箱%' or Material_Desc like '%中封箱%' or Material_Desc like '%普通箱%'", "Package_Mo_Main DESC").CopyToDataTable();
                DataTable dt_MO_Temp = query.Distinct().CopyToDataTable();
                dt_Carton_Mo = Common.DataTableHelper.Distinct(dt_MO_Temp, new string[] { "Package_Mo_Main" });
                for (int k = 0; k < dt_Mo_Main.Rows.Count; k++)
                {
                    if (dt_MO_Temp.Select("Package_Mo_Main='" + dt_Mo_Main.Rows[k]["MO_NO"].ToString() + "'").Length > 0)
                    {
                       DataTable dt_MO_Temp1 = dt_MO_Temp.Select("Package_Mo_Main='" + dt_Mo_Main.Rows[k]["MO_NO"].ToString() + "'").CopyToDataTable();

                        List<CartonMaterial> cartons1 = new List<CartonMaterial>();
                        for (int i = 0; i < dt_MO_Temp1.Rows.Count; i++)
                        {
                            //获取符合条件的物料号
                            string MaterialNo = dt_MO_Temp1.Rows[i]["Material_No"].ToString();
                            //根据物料号查询得到产品尺寸
                            DataTable dt_ProductSize = Common.GetDataTable.Get_SA_CBM_WT_From_SAP_Size(MaterialNo);
                            if (dt_ProductSize.Rows.Count > 0)
                            {
                                string Product_Size = dt_ProductSize.Rows[0]["PRODUCT_DIMENSION"].ToString();
                                string MATERIAL_DESC= dt_ProductSize.Rows[0]["MATERIAL_DESC"].ToString();
                                if (!string.IsNullOrEmpty(Product_Size)|| !string.IsNullOrEmpty(MATERIAL_DESC))
                                {
                                    string strSize = "";
                                    if (string.IsNullOrEmpty(Product_Size))
                                        strSize = MATERIAL_DESC;
                                    else
                                        strSize = Product_Size;
                                    //通过[产品尺寸法CBM解析计算]方法返回[长、宽、高、CBM]
                                    var sizelist = Product_Size_CBM(strSize, 0, 0, 0);
                                    CartonMaterial cartonMaterial = new CartonMaterial();
                                    cartonMaterial.MO_NO = dt_MO_Temp1.Rows[i]["Package_Mo_Main"].ToString();//生产订单
                                    cartonMaterial.CartonDesc = dt_MO_Temp1.Rows[i]["MATERIAL_DESC"].ToString();//描述
                                    cartonMaterial.CartonID = dt_MO_Temp1.Rows[i]["Material_No"].ToString();//纸箱号
                                    cartonMaterial.MaterialQTY = Convert.ToDecimal(dt_MO_Temp1.Rows[i]["Demand_QTY"].ToString());//发料数量
                                    cartonMaterial.CartonCBM = sizelist.Item4;//纸箱CBM
                                    cartonMaterial.VOLUME_LENGTH = sizelist.Item1;//长度
                                    cartonMaterial.VOLUME_WIDTH = sizelist.Item2;//宽度
                                    cartonMaterial.VOLUME_HEIGHT = sizelist.Item3;//高度
                                    cartonMaterial.Base_Unit = dt_MO_Temp1.Rows[i]["Base_Unit"].ToString();//单位
                                    cartonMaterial.CartonSize = strSize;//尺寸
                                    cartonMaterial.Area_MJ = sizelist.Item1 * sizelist.Item2;//面积
                                    cartons1.Add(cartonMaterial);
                                }
                                else
                                {
                                    CartonMaterial cartonMaterial = new CartonMaterial();
                                    cartonMaterial.MO_NO = dt_MO_Temp1.Rows[i]["Package_Mo_Main"].ToString();//生产订单
                                    cartonMaterial.CartonDesc = dt_MO_Temp1.Rows[i]["MATERIAL_DESC"].ToString();//描述
                                    cartonMaterial.CartonID = dt_MO_Temp1.Rows[i]["Material_No"].ToString();//纸箱号
                                    cartonMaterial.MaterialQTY = Convert.ToDecimal(dt_MO_Temp1.Rows[i]["Demand_QTY"].ToString());//发料数量
                                    cartonMaterial.CartonCBM = 0;//纸箱CBM
                                    cartonMaterial.VOLUME_LENGTH = 0;//长度
                                    cartonMaterial.VOLUME_WIDTH = 0;//宽度
                                    cartonMaterial.VOLUME_HEIGHT = 0;//高度
                                    cartonMaterial.Base_Unit = dt_MO_Temp1.Rows[i]["Base_Unit"].ToString();//单位
                                    cartonMaterial.CartonSize = Product_Size;//尺寸
                                    cartonMaterial.Area_MJ = 0;//面积
                                    cartons1.Add(cartonMaterial);

                                }
                            }
                        }
                        //取同一个生产订单下最大的纸箱CBM
                        if (cartons1.Count > 0)
                        {
                            var max_cbm = cartons1.First(p => p.CartonCBM == cartons1.Max(p2 => p2.CartonCBM));
                            cartons.Add(max_cbm);
                        }
                    }
                }
            }
            dt_CartonBom = dt_Carton_Mo;
            return cartons;

        }

        /// <summary>
        /// 解析铝材的长
        /// </summary>
        /// <param name="strProductSize"></param>
        /// <returns></returns>
        public static decimal Product_Size_Length(string strProductSize)
        {
            //转为大写字符串
            strProductSize = strProductSize.ToUpper();
            //采用正则表达式把非数字转换成特殊字符"_"
            string[] list = Regex.Replace(strProductSize, @"[^\d.]", "_").Split('_');
            //取第一个值作为【长】
            List<decimal> sizeList = new List<decimal>();
            decimal Length = 0;
            for (int i = 0; i < list.Length; i++)
            {
                if (list[i].Length > 0)
                    sizeList.Add(Convert.ToDecimal(list[i]));
            }
            if (sizeList.Count >= 1)
            {
                Length = sizeList[0];
            }
            return Length;
        }

    }
}
